//
//  LXPullDownView.m
//  LXLessDemo
//
//  Created by LX on 16/2/12.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXPullDownView.h"
@interface LXPullDownView()
@property(nonatomic,strong) NSString *textString;
@property(nonatomic,strong) UIImage *downImage;
@property(nonatomic,strong) UIImage *upImage;
@property(nonatomic,strong) UIImageView *dowmImageView;
@property(nonatomic,assign) BOOL isSelect;

@end
@implementation LXPullDownView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)initWithFrame:(CGRect)frame downImage:(UIImage *)downImage upImage:(UIImage *)upImage text:(NSString *)text{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.downImage = downImage;
        self.upImage = upImage;
        self.textString = text;
        self.isSelect = NO;
        [self addMainView];
    }
    return self;
}
-(void)addMainView{
    
    UILabel *lab = [LXCommonUtils settingLabWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)) text:self.textString textColor:[UIColor whiteColor] fount:firstTextFont];
    lab.center = CGPointMake(CGRectGetWidth(lab.bounds)/2.0, CGRectGetHeight(self.bounds)/2.0);
    [self addSubview:lab];
    
    self.dowmImageView = [[UIImageView alloc] initWithImage:self.downImage];
    [self.dowmImageView sizeToFit];
    self.dowmImageView.center = CGPointMake(CGRectGetMaxX(lab.frame) + CGRectGetWidth(self.dowmImageView.bounds)/2.0 + 2, lab.center.y);
    [self addSubview:self.dowmImageView];
    
    CGRect frame = self.frame;
    frame.size.width = CGRectGetMaxX(self.dowmImageView.frame);
    self.frame = frame;

    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectClick:)]];
}
-(void)selectClickWithIsSelect:(BOOL)isSelect{
    if (isSelect) {
        [self.dowmImageView setImage:[UIImage imageNamed:@"up"]];
    }else{
        [self.dowmImageView setImage:[UIImage imageNamed:@"down"]];
    }
}
-(void)selectClick:(UITapGestureRecognizer*)sender{
    self.isSelect = !self.isSelect;
    if (self.selectBlock) {
        self.selectBlock(self.isSelect);
    }
    [self.dowmImageView setImage:self.isSelect ? [UIImage imageNamed:@"up"]:[UIImage imageNamed:@"down"]];

}
@end
